<template>
  <div class="resourceinformation">
    <el-form ref="form" :model="form" label-width="100px">
      <div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="资源名称:" prop="resourceName">
              <span>{{form.resourceName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="年级类型:" prop="gradeType">
              <el-select style="width: 100%" v-model="form.gradeType" disabled placeholder="请选择年级类型">
                <el-option
                  v-for="dict in dict.type.grade_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学科类型:" prop="subjectType">
              <el-select style="width: 100%" v-model="form.subjectType" disabled placeholder="请选择学科类型">
                <el-option
                  v-for="dict in dict.type.subject_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="封面图片:" prop="resourceImg">
              <img style="width: 120px" :src="form.resourceImg" alt="">
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源备注:" prop="remarks">
              <span>{{form.remarks}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <el-row>
        <el-col :span="4">
          <el-form-item label="文件列表" prop="fileList">
            <el-button type="primary" size="small" @click="openFile"><i class="el-icon-plus"></i>选取文件</el-button>
            <!--              <file-upload v-modal="form.resourceFiles"/>-->
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="资源名称" >
            <el-input
              v-model="query.resourceName"
              placeholder="请输入资源名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="文件类型" >
            <el-select style="width: 100%" v-model="query.fileType" placeholder="请选择文件类型">
              <el-option
                v-for="item in fileTypeLi"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table v-loading="loading" :data="form.resourceFiles"  >
            <!--              <el-table-column type="index" width="50"></el-table-column>-->
            <el-table-column label="资源名称" align="center" prop="resourceName" />
            <el-table-column label="资源详情" align="center" prop="resourceDetails" />
            <el-table-column label="文件" align="center" show-overflow-tooltip prop="subjectType">
              <template slot-scope="scope">
                <div>{{scope.row.resourceUrl}}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleView(scope.row)"
                  v-hasPermi="['classai:resource:edit']"
                >
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['classai:resource:remove']"
                >
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="query.pageNum"
            :limit.sync="query.pageSize"
            @pagination="getFileList"
          />
        </el-col>
      </el-row>
    </el-form>
<!--    <div slot="footer" class="dialog-footer">-->
<!--      <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--      <el-button @click="cancel">取 消</el-button>-->
<!--    </div>-->
    <el-dialog title="文件上传" :visible.sync="open1" width="60%" append-to-body>
      <el-form ref="form1" :model="resourceFiles" :rules="rules1" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源名称" prop="resourceName">
              <el-input v-model="resourceFiles.resourceName" placeholder="请输入资源名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源详情" >
              <el-input v-model="resourceFiles.resourceDetails" placeholder="请输入资源详情" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件上传" prop="resourceUrl">
              <file-upload :fileSize="50" v-model="resourceFiles.resourceUrl" :file-type="['mp4', 'wmv', 'avi', 'mpeg', 'png', 'jpg', 'jpeg', 'mp3']"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" width="60%">
      <div class="dialogContent">
        <div style="display: flex; align-items: center; justify-content: center">
          <video
            v-if="videoTypes.includes(dataDetailLi.fileDetails)"
            ref="videoRef"
            :src="dataDetailLi.resourceUrl"
            controls
            width="600"
          >
            您的浏览器不支持 video 标签。
          </video>
          <img
            v-if="imgTypes.includes(dataDetailLi.fileDetails)"
            :src="dataDetailLi.resourceUrl"
            style="width: 400px;"
            alt=""
          />

          <audio v-if="audioTypes.includes(dataDetailLi.fileDetails)" controls>
            <source :src="dataDetailLi.resourceUrl" type="audio/mpeg" />
            您的浏览器不支持 audio 元素。
          </audio>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listResource,
  getResource,
  delResource,
  addResource,
  updateResource,
  getResourceInfo, addRresourcedetails, delResourcedetails
} from "@/api/classai/resource";
import {listResourcedetails} from "@/api/classai/resourcedetails";

export default {
  name: "resourceinformation",
  dicts: ['grade_type', 'subject_type'],
  data() {
    return {
      videoTypes : ['mp4', 'wmv', 'avi', 'mpeg'],
      imgTypes : ['png', 'jpg', 'jpeg'],
      audioTypes : ['mp3'],
      typesAll : ['mp4', 'wmv', 'avi', 'mpeg', 'png', 'jpg', 'jpeg', 'mp3'],
      dataDetailLi:{},
      fileTypeLi:[
        {
          value:0,
          label:'图片'
        },
        {
          value:1,
          label:'视频'
        },
        {
          value:2,
          label:'音频'
        },
      ],
      query:{
        pageNum: 1,
        pageSize: 10,
        fileType:null,
        resourceName:'',
        resourceId:''
      },
      dialogVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源库表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        resourceName: null,
        remarks: null,
        resourceImg: null,
        browseNum: null,
        gradeType: null,
        subjectType: null
      },
      resourceFiles:{},
      // 表单参数
      form: {
        resourceFiles: [
          {
            resourceUrl:'',
            resourceName:'',
            resourceDetails:'',
          }
        ],
      },
      // 表单校验
      rules1: {
        resourceName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
        resourceUrl: [
          { required: true, message: "资源地址不能为空", trigger: "change" }
        ],
      }

    };
  },
  created() {
    this.query.resourceId = this.$route.query.id
    this.getList();
  },
  methods: {
    // 打开选取文件弹窗
    openFile(){
      this.open1 = true
    },
    /** 查询资源库列表 */
    getList() {
      this.loading = true;
      const id =this.$route.query.id
      // getResourceInfo
      getResource(id).then(response => {
        this.form = response.data;
        this.resourceFiles.resourceId = response.data.id
        // this.form.resourceFiles = response.data.resourceDetails
        this.loading = false;
        this.getFileList()
      });
    },

    getFileList() {
      // {id: this.$route.query.id }
      listResourcedetails(this.query).then(res => {
        this.form.resourceFiles = res.rows
        this.total = res.total;
        this.$forceUpdate()
      })
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.dataDetailLi = {}
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    reset1(){
      this.resourceFiles = {
        resourceUrl:'',
        resourceName:'',
        resourceDetails:'',
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        resourceName: null,
        remarks: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        resourceImg: null,
        resourceFiles: null,
        browseNum: null,
        status: null,
        gradeType: null,
        subjectType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getFileList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.query.fileType = null
      this.query.resourceName = ''
      this.getFileList();
    },
    handleDetails(row){
      this.reset();
      const ids = row.id || this.ids
      const routeData = this.$router.resolve({
        path: '/resourceinformation',
        query: { id: ids } // 可选
      })
      window.open(routeData.href, '_blank') // _blank 表示新标签页
      // this.$router.push({ path: '/resourcedetails', query: { id: 123 } })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResource(id).then(response => {
        this.form = response.data;
        this.form.resourceFiles = response.data.resourceDetails
        this.open = true;
        this.title = "修改资源库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.resourceFiles,'------')
          addRresourcedetails(this.resourceFiles).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open1 = false;
            this.reset1()
            this.getList();
          });
        }
      });
    },
    /** 资源地址提交按钮 */
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {

          if (!this.form.resourceFiles) {
            this.form.resourceFiles = [this.resourceFiles]
          } else {
            this.form.resourceFiles.push(this.resourceFiles)
          }
          this.open1 = false
          this.reset1()
        }
      });
    },
    handleView(e) {
      this.dataDetailLi = e
      this.dialogVisible = true
      // window.open(e.resourceUrl, '_blank')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资源库详情编号为"' + ids + '"的数据项？').then(function() {
        return delResourcedetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('classai/resource/export', {
        ...this.queryParams
      }, `resource_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.resourceinformation{
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #333 !important;
  background-color: #ffffff !important;
  border: none !important;
  padding: 0;
}
::v-deep .el-textarea.is-disabled .el-textarea__inner{
  background-color: #ffffff !important;
  border: none !important;
  color: #333 !important;
}
::v-deep .el-input.is-disabled .el-input__icon {
  display: none;
}
</style>
